package com.trainoo.service;

import com.trainoo.entity.ElasticEntity;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.MatchQuery;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <p> add description here </p>
 *
 * @author tao.zhou
 * @version 1.0
 * @date 2020/6/16 16:34:00
 */
@Slf4j
@Component
public class BaseElasticService {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    public boolean checkIndex(String indexName) throws IOException {

        // GetIndexRequest requestAll = new GetIndexRequest().indicesOptions(IndicesOptions.STRICT_EXPAND_OPEN);
        // for (String index : restHighLevelClient.indices().get(requestAll, RequestOptions.DEFAULT).getIndices()) {
        //     log.info(">>> " + index);
        // }

        GetIndexRequest request =  new GetIndexRequest(indexName);
        return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
    }

    public void index(String indexName) throws IOException {
        IndexRequest request =  new IndexRequest(indexName);
        request.id(ElasticEntity.getEntity().getId());
        request.source(ElasticEntity.getEntity().getData());
        restHighLevelClient.index(request, RequestOptions.DEFAULT);
    }

    public void createIndex(String indexName) throws IOException {
        CreateIndexRequest request =  new CreateIndexRequest(indexName);
        request.source(ElasticEntity.getEntity().getData());
        restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        log.info("createIndex");
    }

    public void getIndex(String indexName) throws IOException {
        GetIndexRequest request =  new GetIndexRequest(indexName);
        GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
    }

    public void delIndex(String indexName) {
        DeleteIndexRequest request =  new DeleteIndexRequest(indexName);
        try {
            AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            log.error("index 不存在");
        }
    }

    public void bluk(String indexName) throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        ElasticEntity.getListEntity().forEach(item ->
                bulkRequest.add(new IndexRequest(indexName).id(item.getId()).source(item.getData())));

        restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    }

    public void searchIndex(String indexName) throws IOException {
        SearchRequest request =  new SearchRequest(indexName);

        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchQuery("desc", "best"));

        request.source(builder);

        SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);

        SearchHit[] hits = searchResponse.getHits().getHits();
        if (hits!= null  && hits.length > 0) {
            for (SearchHit hit : hits) {
                log.info(hit.getIndex());
                log.info(hit.getId());
                log.info("" + hit.getScore());
                log.info(hit.getSourceAsString());
                log.info("" + hit.getSourceAsMap());
            }
        }
    }
}
